##########################################################################
#File Name:sysmonitor.sh
#Author:hongxing
#Desc:获取操作系统的基本信息,获取系统的运行状态
##########################################################################

##!/bin/sh

os_release_info() {
    OS=`uname -s`
    REV=`uname -r`
    MACH=`uname -m`
    
    if [ "${OS}" = "SunOS" ] ; then
        OS=Solaris
        ARCH=`uname -p`
        OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
    elif [ "${OS}" = "AIX" ] ; then
        OSSTR="${OS} `oslevel` (`oslevel -r`)"
    elif [ "${OS}" = "Linux" ] ; then
        KERNEL=`uname -r`
        if [ -f /etc/redhat-release ] ; then
            DIST='RedHat'
            PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
            REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
        elif [ -f /etc/SuSE-release ] ; then
            DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
            REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
        elif [ -f /etc/mandrake-release ] ; then
            DIST='Mandrake'
            PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
            REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
        elif [ -f /etc/os-release ]; then
        DIST=`awk -F "PRETTY_NAME=" '{print $2}' /etc/os-release | tr -d '\n"'`
        elif [ -f /etc/debian_version ] ; then
            DIST="Debian `cat /etc/debian_version`"
            REV=""

        fi
        if ${OSSTR} [ -f /etc/UnitedLinux-release ] ; then
            DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
        fi

        OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"
    fi

    echo $OSSTR
}
#获取操作系统基本信息
sys_info() {
    echo -e "\E[33m############################# system infomation #############################\033[0m"
    echo -e '\E[32m'"OS Type :        " $tecreset `uname -o`
    echo -e '\E[32m'"OS Version :     " $tecreset `os_release_info`
    echo -e '\E[32m'"Architecture :   " $tecreset `uname -m`
    echo -e '\E[32m'"Kernel Release : " $tecreset `uname -r`
    echo -e '\E[32m'"Hostname :       " $tecreset $HOSTNAME
    echo -e '\E[32m'"Internal IP :    " $tecreset `hostname -I`
    echo -e '\E[32m'"External IP :    " $tecreset `curl -s ipecho.net/plain;echo`
    echo -e '\E[32m'"Name Servers :   " $tecreset `cat /etc/resolv.conf | sed '1 d' | awk '{print $2}'` 
    echo -e '\E[32m'"Logged In users :" $tecreset 
    who 
    echo -e "\E[33m############################################################################\033[0m"
}

#获取系统的运行状态
sys_status() {
    
    echo -e "\E[33m############################# system status ################################\033[0m"
    echo -e '\E[32m'"Time Now :" $tecreset `date`
    echo -e '\E[32m'"Ram Usages :" $tecreset 
    free -h | grep -v "Swap"
    echo -e '\E[32m'"Swap Usages :" $tecreset 
    free -h | grep -v "Mem"
    echo -e '\E[32m'"Disk Usages :" $tecreset  
    df -hP  | grep -vE 'tmpfs'
    echo -e '\E[32m'"Load Average :" $tecreset `uptime | awk '{print $10 $11 $12}' `
    echo -e '\E[32m'"System Uptime :" $tecreset `uptime | awk '{print $3,$4,$5}' | cut -f1,2 -d,`
    echo -e '\E[32m'"Top memory using processs :" $tecreset
    top -b -n1 -o +%MEM | tail -n+7 | head -n 11
    echo -e '\E[32m'"Top CPU using process :" $tecreset 
    top -b -n1 -o +%CPU | tail -n+7 | head -n 11
    echo -e '\E[32m'"Shared Memory Segments :" $tecreset 
    ipcs -m | tail -n+3
    echo -e '\E[32m'"Semaphore Arrays :" $tecreset 
    ipcs -s | tail -n+3
    echo -e "\E[33m############################################################################\033[0m"

}

dbs_info() {
    echo -e "\E[33m############################# DBS infomation ###############################\033[0m"
    echo -e '\E[32m'"DBS Process :" $tecreset 
    ps -aux | grep -E 'sqlr|datasync' | grep -v grep
    #ps -ef | grep -E 'sqlr|datasync' | sed '$d' 
    echo -e '\E[32m'"DBS Network :" $tecreset
    # TODO 没有使用root或sudo执行脚本,则netstat命令 -p无法显示某些端口的进程信息
    netstat -antp | grep 'sqlr'
    echo -e "\E[33m############################################################################\033[0m"
}


#判断当前网络的连通性，直接使用ping命令测试
#ping -c 2 www.ifeng.com &>/dev/null && echo "网络连通:yes"||echo "网络连通:no"

usage() {
    echo "Usage: $0 [base|status|dbs]"
    echo "base:  show OS base infomatioin"
    echo "satus: show OS running status"
    echo "dbs:   show DBS infomation"
    echo "       no argument will show all"
    exit
}

if [ $# -gt 1 ]; then 
    usage 
elif [ $# -eq 1 ]; then 
    if [ "$1" == "base"  -o  "$1" == "status" -o  "$1" == "dbs" ]; then 
        clear
    else 
        usage
    fi
fi 

clear
#定义一个高亮输出的变量
tecreset=$(tput sgr0)

if [ $# -eq 0 ]; then 
    sys_info
    printf '\n'
    sys_status
    printf '\n'
    dbs_info
else 
    if [ "$1" == "base" ]; then
        sys_info
    elif [ "$1" == "status" ]; then
        sys_status
    elif [ "$1" == "dbs" ]; then
        dbs_info
    fi
fi 

unset tecreset    





